#
# pH+ Application class.
#
require 'ph/core'

module Ph
  
  class Application

		# Sets Current Application Mode. ie: :development, :test, etc
		def mode(mode)
			@mode = mode
		end

		# Sets Router. Default : Ph::Router
		def router(router)
			@router = router
		end

		def controller(symbol, klass)
			@router.routes[symbol] = klass
		end

		def initialize(&block)
				@controllers = Hash.new
				@router = Ph::Router.new
				instance_eval(&block)
		end

		# TODO Make it use Response
		def error(error, backtrace)
			[500, {'Content-Type' => 'text/html' }, 
				"<h1>500 - Internal Error</h1><pre>#{error}\n#{backtrace}</pre>"]
		end

		def call(env)
			env[:application] = self

			return @router.route(env) 

		rescue
			self.error($!, $@)
		end

  end  
  
end
